﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Tickettoticket;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.InvoicesandCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.InvoicesandCommandHandler
{
    /// <summary>
    /// 开票到票采购订单
    /// 物联网-智能MES生产管理系统-07-开票到票管理V1.1-20250206
    /// </summary>
    public class AddicetsCommandHander : IRequestHandler<AddicetsCommand, APIResult<object>>
    {

        /// <summary>
        /// 开票到票
        /// </summary>
        private readonly IBaseRepository<Billingder> billingderRepository;

        /// <summary>
        /// 销售订单
        /// </summary>
        private readonly IBaseRepository<urchaseOrdp> urchaseOrdpRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        private readonly SDbContext db;

        public AddicetsCommandHander(IBaseRepository<Billingder> billingderRepository, IBaseRepository<urchaseOrdp> urchaseOrdpRepository, IMapper mapper, SDbContext db)
        {
            this.billingderRepository = billingderRepository;
            this.urchaseOrdpRepository = urchaseOrdpRepository;
            this.mapper = mapper;
            this.db = db;
        }



        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<object>> Handle(AddicetsCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            using (var transaction = await db.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    AddicetsCommand add = new AddicetsCommand();
                    add.Bid = request.Bid;
                    add.BillingStae = request.BillingStae;
                    add.Billingtopic = request.Billingtopic;
                    add.Billingtype = request.Billingtype;
                    add.BillingDate = request.BillingDate;
                    add.Billingsale = request.Billingsale;
                    add.BillTicket = request.BillTicket;
                    add.number = request.number;
                    add.remark = request.remark;
                    var list = mapper.Map<Billingder>(add);
                    int ane = billingderRepository.Add(list).Result;
                    if (ane > 0)
                    {
                        request.urchaseOrdps.ForEach(x =>
                        {
                            x.UrcStae = list.BillingStae;
                        });
                        int add2 = urchaseOrdpRepository.AddRanges(request.urchaseOrdps).Result;
                        if (add2 > 0)
                        {
                            result.Code = APIEnum.添加成功;
                            result.Msg = APIEnum.添加成功.ToString();

                            //提交事务
                            await transaction.CommitAsync(cancellationToken);
                        }
                        else
                        {
                            result.Code = APIEnum.添加失败;
                            result.Msg = APIEnum.添加失败.ToString();

                            //回滚事务
                            await transaction.RollbackAsync(cancellationToken);
                        }

                    }
                }
                catch (Exception ex)
                {

                    // 输出详细的错误信息
                    string errorMessage = $"添加失败: {ex.Message}";
                    if (ex.InnerException != null)
                    {
                        errorMessage += $" 内部异常: {ex.InnerException.Message}";
                    }
                    result.Code = APIEnum.添加失败;
                    result.Msg = errorMessage;
                    await transaction.RollbackAsync(cancellationToken);
                }

                
           
            }
            return result;
        }
    }
}
